#region - Class details -
/*  
 *  Developer   : Wicus Botha
 *  Description : 
 *  History     : WB - 2008-01-25 - Created
 */
#endregion

using System;
using System.Drawing;
using System.Windows.Forms;
using IQ.BaseWindows;
using IQ.DbBase;

namespace IQ.WinForms
{
	/// <summary>
	/// Description of frmSetupConnection.
	/// </summary>
	public partial class frmSetupConnection : Form
    {
        #region # Constructor #
        public frmSetupConnection()
		{
			InitializeComponent();
			connection = new ConnectionProperties();
		}
        #endregion

        #region - Private variables -
        private ConnectionProperties connection;		
		#endregion
		
		#region - Private methods -
		private void SetConnectionProps()
		{
			Connection.Name     = txtConName.Text;
			Connection.Server	= txtServer.Text;
			Connection.Database = txtDB.Text;
			Connection.UserID 	= txtUserID.Text;
			Connection.Password = txtPassword.Text;
			Connection.IntegratedSecurity = cbxSecurity.Checked;
			if (txtDriver.Text.Trim() != "")
				Connection.Driver = txtDriver.Text;
			if (txtPort.Text.Trim() != "")
				Connection.Port = txtPort.Text;
		}
		private void LoadConnectionToForm()
		{
			txtConName.Text  = Connection.Name;
			txtServer.Text   = Connection.Server;
			txtDB.Text       = Connection.Database;
			txtUserID.Text   = Connection.UserID;
			txtPassword.Text = Connection.Password;
			txtDriver.Text   = Connection.Driver;
			txtPort.Text     = Connection.Port;
		}
		private void TestConnection()
		{
			SetConnectionProps();
			string conError = Connection.TestConnectionString();
			if (Connection.CanConnect)
				General.MesDlg("Connection successfull!",'I');
			else
				General.MesDlg("Connection fail: " + conError ,'E');
		}		
		#endregion
		
		#region - Public properties / variables -
		public ConnectionProperties Connection
		{
			get { return this.connection; }
			set { this.connection = value;
				LoadConnectionToForm();
				}				
		}
		#endregion
		
		#region - Public methods -
		public void LockConnectionType(ConnectionTypes connectType)
		{
            switch (connectType)
            {
                case ConnectionTypes.SqlServer: cbxCt.SelectedIndex = 0; break;
                case ConnectionTypes.OleDB: cbxCt.SelectedIndex = 1; break;
                case ConnectionTypes.Odbc: cbxCt.SelectedIndex = 2; break;
            }
			cbxCt.Enabled = false;
            txtDriver.Enabled = (cbxCt.SelectedIndex == 2);
            txtPort.Enabled = (cbxCt.SelectedIndex == 2);
        }
		#endregion

        #region - Control Methods -
        private void btnTestClick(object sender, EventArgs e)
        {
            TestConnection();
        }
        private void cbxCtSelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxCt.SelectedIndex == 0)
                Connection.ConnectionType = ConnectionTypes.SqlServer;
            else
                Connection.ConnectionType = ConnectionTypes.Odbc;
        }
        private void btnCancelClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
        private void btnOkClick(object sender, EventArgs e)
        {
            TestConnection();
            if (!connection.CanConnect)
            {
                if (General.MesDlg("Connection test unsuccessful, close form anyway?", 'X')
                    == DialogResult.Yes)
                    this.DialogResult = DialogResult.No;
            }
            else
                this.DialogResult = DialogResult.OK;
        }
        #endregion
	}
}
